<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class AdminBaseController extends Controller
{
    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout    = '//admin/admin_column';
    public $themeName = 'basic'; 

    // menus
    public $menus;

    // locale manager
    public $locale;

    // url manager
    public $url;
    
    // 分頁管理
    public $pageLimit = null;

    // 權限管理
    public $permissionManager = null;

    // session 管理
    public $sessionManager = null;

    // service 管理
    public $serviceManager = null;

    // user object
    public $user = null;

    /*
    */
    function init()
    {
        parent::init();

        /**
         *  init information
         */
        //$this->smarty   = Yii::app()->gmanager->get('smarty');
        //$this->doctrine = Yii::app()->gmanager->get('doctrine');
        //Yii::app()->gmanager->get('doctrine');
        //Yii::app()->gmanager->get('zend');
        
        $this->initDb();


        Yii::import('application.libraries.menus.*');
        $this->menus = new Menus();

        // base load
        //$dateManager = new DateManager();
        $this->locale = new LocaleManager();
        $this->locale->loadLocale();
        $this->url = new UrlManager( $this ); // $this is controller  

        //
        $this->sessionManager = new SessionManager();
        $this->user = $this->sessionManager->getUser();

        //
        $this->serviceManager = new ServiceManager();
        $this->serviceManager->setSessionManager( $this->sessionManager );

        //
        $this->permissionManager = new PermissionManager();
        $this->permissionManager->setSessionManager( $this->sessionManager );

    }

    /**
     *  檢查權限
     */
    /*
    public function checkPermissions()
    {
        
    }
    */

    function beforeAction($action)
    {
        //parent::beforeAction($action);


        // check user identities
        $user = $this->sessionManager->getUser();
        if( !$user ) {
            Request::redirect('member/');
            //Request::redirect('member/index');
        }

        //
        $this->sessionManager->clearTopMessages();

        // set menu, menu item focus
        $controller = $action->getController();
        $this->menus->setFocusByAction( $controller->getId() .'/'. $action->getId() );

        return true;
    }

    function afterAction($action)
    {
/*
        // 設定顯示方式
        $this->smarty->assign("smarty_time", sprintf('%0.5f',Yii::getLogger()->getExecutionTime()));
        $this->smarty->assign("smarty_memory", round(Yii::getLogger()->getMemoryUsage()/(1024*1024),2)." MB");
        $this->smarty->assign('yii', Yii::app());

        if( $this->_displayKey == YII_DEFAULT_KEY ) {
            $this->smarty->display( $action->controller->id .'_'. $action->id .'.tpl' );
        }
        elseif( $this->_displayKey == '' ) {
            // nothint
        }
        else {
            $this->smarty->display( 
                strtolower( join('_', preg_split( '/[\/]/', $this->_displayKey ) ) ) .'.tpl' 
            );
        }
        // End
*/



        //parent::afterAction($action);
        return true;
    }

    // --------------------------------------------------------------------------------
    //
    // --------------------------------------------------------------------------------

    // template base path
    public function getTemplatePath( $templateName=null, $pathType=null )
    {
        $basePath = UrlManager::getBasePath($pathType);
        if( $templateName ) {
            $templatePath = $basePath .'/templates/admin/'. $this->themeName .'/'. $templateName;
        }
        else {
            $templatePath = $basePath .'/templates/admin/'. $this->themeName;
        }
        return $templatePath;
    }


}